package cn.aopmin.listener;

import cn.aopmin.event.LikedTaskEvent;
import cn.aopmin.utils.RedisUtils;
import cn.hutool.core.util.BooleanUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 点赞监听器
 * @author 白豆五
 * @since 2025/3/15
 */
@Slf4j
@Component
public class LikedTaskEventListener {

    @Order(1) // 优先级
    @Async // 异步执行
    @EventListener
    public void handleEvent(LikedTaskEvent event) {
        log.info("点赞事件监听器执行");
        Map<String, Object> map = event.getEntity();
        String key = "liked:bookId:" + map.get("id");
        Boolean member = RedisUtils.sIsMember(key, map.get("userId"));
        if (BooleanUtil.isTrue(member)) {
            RedisUtils.sRemove(key, map.get("userId"));
        } else {
            RedisUtils.sSetAndTime(key, 365 * 24 * 60 * 60L, map.get("userId"));
        }
    }
}